require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIArenaWinReward = {};
HelperFunc.SetUIEnv(UIArenaWinReward)
local this = UIArenaWinReward;

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;
this.backFunction = nil;

this.hasInit = false;
this.infos = {};
this.btnBack = nil;
this.listItemGrid = nil;

this.arenaBaseInfo = nil;
this.items = {};
this.winRewardConfig = nil;

function UIArenaWinReward.Init(backAct, _arena_type)--todo 应该小写 暂时先大写
    backFunction = backAct;
    this.cur_arena_type = _arena_type or EnumConst.ArenaBattleType.Normal
    if this.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        winRewardConfig = G.dataTable["t_arenaboxreward"]
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        winRewardConfig = G.dataTable["t_arenaboxrewardcs"]
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        winRewardConfig = G.dataTable["t_arenaboxreward"]
    else
        error("错误竞技场类型")
    end
    local name, packageName = getUIName(PanelResNames.UIArenaWinReward)
    panelMgr:Push(name, packageName, this.onCreate,Config.useLocalUI)
end

function UIArenaWinReward.onCreate(obj)
    if not hasInit then
        hasInit = true;
        btnBack = obj:GetChild("BackButton")
        listItemGrid = obj:GetChild("itemGrid")
        this.addEvent()
    end
    this.refresh()
end

function UIArenaWinReward.addEvent()
    btnBack.onClick:Add(this.onClickClose)
end

function UIArenaWinReward.removeEvent()
    btnBack.onClick:Clear();
end

function UIArenaWinReward.LocalListenEvent()

end

function UIArenaWinReward.LocalCloseEvent()

end

function UIArenaWinReward.OnDestroy()
    hasInit = false
    winRewardConfig = nil
    this.removeEvent()
end

function UIArenaWinReward.OnEnable()
    this.LocalListenEvent();
end

function UIArenaWinReward.OnDisable()
    this.LocalCloseEvent();
end


function UIArenaWinReward.refresh()
    if this.cur_arena_type == EnumConst.ArenaBattleType.Normal then
        this.refreshWithData(logicMgr.ArenaManager.getBaseInfo())
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.Cross then
        this.refreshWithData(logicMgr.ArenaManager.getCrossBaseInfo())
    elseif this.cur_arena_type == EnumConst.ArenaBattleType.TeamCross then
        this.refreshWithData(logicMgr.ArenaManager.getCrossTeamBaseInfo())
    else
        error("错误竞技场类型")
    end
end

function UIArenaWinReward.refreshWithData(baseInfo)
    arenaBaseInfo = baseInfo
    infos = {}
    for key, value in pairs(winRewardConfig) do
        table.insert(infos,value)
    end
    table.sort(infos,function (a,b)
        return a.f_Id<b.f_Id
    end);
    listItemGrid.itemRenderer = this.setData
    listItemGrid.numItems = #infos
end

function UIArenaWinReward.setData(index,obj)
    local data = infos[index + 1]
    local txtWinCount = obj:GetChild("WinCount")
    local objContent = obj:GetChild("Content")
    local btnReward = obj:GetChild("RewardBtn")
    if arenaBaseInfo.winCount>=data.f_winCount  then
        txtWinCount.text =string.TextColor( arenaBaseInfo.winCount.."/"..data.f_winCount,"#00FF19")
        btnReward.visible = not logicMgr.ArenaManager.IsGetReward(data.f_Id)
    else
        txtWinCount.text =string.TextColor( arenaBaseInfo.winCount.."/"..data.f_winCount,"#FF1C00")
        btnReward.visible = false
    end

    items = decodeJsonStrToTabel(data.f_rewards)
    objContent.itemRenderer = this.setItemData
    objContent.numItems = #items
end

function UIArenaWinReward.setItemData(index,obj)
    local itemCg = logicMgr.ItemManager.getItemConstDatabyModelId(items[index + 1][1])
    itemCg.num = items[index + 1][2]
    local itemCtr = CtrlManager.GetCtrl(PanelNames.Item)
    itemCtr.initData(obj,itemCg)
    obj.onClick:Clear()
    obj.onClick:Add(function()
        this.onClickItemReward()
    end);
end

function UIArenaWinReward.onClickItemReward(go,data)
    log("UIArenaWinReward.onClickItemReward")
end

function UIArenaWinReward.onClickClose()
    closeUI(PanelNames.UIArenaWinReward)
    if backFunction ~= nil then
        backFunction()
    end
end

return UIArenaWinReward;
